<template>
  <div>
    <div class="table-page-search-wrapper">
      <a-row :gutter="48">
        <a-col :md="20" :sm="60">
        </a-col>
        <a-col :md="4" :sm="12">
          <a-button type="primary" @click="$refs.table.refresh(true)">刷新</a-button>
        </a-col>
      </a-row>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="handleEdit()">新增</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
        </template>
        <a-dropdown>
          <a class="ant-dropdown-link">
            更多 <a-icon type="down" />
          </a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a @click="handleDelete(record)">删除</a>
            </a-menu-item>
          </a-menu>
        </a-dropdown>
      </span>
    </s-table>
  </div>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import { getUserList, deleteUser } from '@/api/user'

export default {
  name: 'TableList',
  components: {
    STable
  },
  data () {
    return {
      mdl: {},
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        // {
        //   title: '序号',
        //   scopedSlots: { customRender: 'serial' }
        // },
        // {
        //   title: '#ID',
        //   dataIndex: 'id',
        //   width: 2
        // },
        {
          title: '登录名',
          dataIndex: 'code'
        },
        {
          title: '姓名',
          dataIndex: 'name'
        },
        {
          title: '录入时间',
          dataIndex: 'createdTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log('loadData.parameter', parameter)
        return getUserList(parameter)
          .then(res => {
            return res
          })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created () {
  },
  methods: {

    handleEdit (record) {
      this.$emit('onEdit', record)
    },
    handleDelete (record) {
      deleteUser(record.id).then(res => {
        this.$refs.table.refresh(true)
        this.$message.info('删除成功')
      }).catch(err => {
        this.$message.error(err.response.data.message)
      })
    },
    handleOk () {

    },

    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
